import {defineStore} from 'pinia'
import {message} from 'ant-design-vue';

import zjRequest from "@/services";
import {IActions, IState, IStateItem, URL_TYPE} from "@/store/adminModule/types";
import {IResponseData} from "@/services/types";
import {ADMIN_LIST_DEFAULT_PAGE_SIZE, PROXY_FRONT_URL} from "@/constants/data";

const useAdminStore = defineStore<any, IState, any, IActions>('adminModule', {
    state: () => {
        return {
            adminInfo: {
                data: [],
                page_info: {
                    current_page: 1,
                    page_size: ADMIN_LIST_DEFAULT_PAGE_SIZE,
                    total_count: 0
                }
            },
            currentAdminDetail: {
                id: 0,
                username: "",
                password: "",
                permission: 1,
                avatar_url: ""
            }
        }
    },
    actions: {
        async requestAdminDetailAction(params) {
            const res: IResponseData = await zjRequest.get({
                url: URL_TYPE.ADMIN_DETAIL,
                params
            })
            if (res.code === 0) {
                this.currentAdminDetail = res.data
                this.currentAdminDetail.avatar_url = PROXY_FRONT_URL + this.currentAdminDetail.avatar_url
            }
        },
        // 获取管理员列表
        async requestAdminListAction(params) {
            const res: IResponseData = await zjRequest.get({
                url: URL_TYPE.ADMIN_LIST,
                params
            })
            if (res.code === 0) {
                this.adminInfo.data = res.data.data.map((item: IStateItem) => {
                    return item
                })
                this.adminInfo.page_info = res.data.page_info
            }
        },
        // 添加管理员
        async addAdminAction(params) {
            const res: IResponseData = await zjRequest.post({
                url: URL_TYPE.ADMIN_ADD,
                data: params
            })
            if (res.code === 0) { // 添加成功
                message.success("添加成功")
                this.requestAdminListAction({
                    current_page: 1,
                    page_size: this.adminInfo.page_info.page_size
                })
                return true
            } else {
                return false
            }
        },
        // 删除管理员
        async deleteAdminAction(params) {
            const res = await zjRequest.post({
                url: URL_TYPE.ADMIN_DELETE,
                data: params
            })
            if (res.code === 0) {// 删除成功
                message.success("删除成功")
                this.requestAdminListAction({
                    current_page: 1,
                    page_size: this.adminInfo.page_info.page_size
                })
                return true
            } else {
                return false
            }
        },
        // 修改管理员
        async updateAdminAction(params) {
            const res = await zjRequest.post({
                url: URL_TYPE.ADMIN_UPDATE,
                data: params
            })
            if (res.code === 0) {
                message.success("修改成功")
                this.requestAdminListAction({
                    current_page: 1,
                    page_size: this.adminInfo.page_info.page_size,
                })
                return true
            } else {
                return false
            }
        },
        async updateAdminSelfAction(params) {
            const res: IResponseData = await zjRequest.post({
                url: URL_TYPE.ADMIN_UPDATE_SELF,
                data: params
            })
            if (res.code === 0) {
                message.success("修改成功")
                return true
            } else {
                return false
            }
        }
    }
})
export default useAdminStore
